﻿Imports System.Web.UI.WebControls
Imports System.Web.UI

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class WhatsNew
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Implements DotNetNuke.Entities.Modules.IActionable
        Private fi As Forum

#Region "DNN Actions"
        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New DotNetNuke.Entities.Modules.Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, Utilities.GetSharedResource("Configure"), DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property
#End Region
        Public Sub New()
       
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim timeOffset As Integer = 0
            timeOffset = PortalSettings.TimeZoneOffset
            If UserId > 0 Then
                Dim dnnUser As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo()
                timeOffset = dnnUser.Profile.TimeZone
                If timeOffset = 0 Then
                    timeOffset = PortalSettings.TimeZoneOffset
                End If
            End If
            Dim lit As New Literal
            Dim sb As New Text.StringBuilder
            Dim forumids As String = ""
            Dim TopicsOnly As Boolean = False
            Dim RandomOrder As Boolean = False
            Dim Header As String = ""
            Dim Template As String = ""
            Dim Footer As String = ""
            Dim Rows As Integer = 10
            Dim bRSS As Boolean = False
            Dim bRSSSecurity As Boolean = False
            Dim bRSSIncludBody As Boolean = False
            Dim Tags As String = ""
            Dim obj As Object = DataCache.CacheRetrieve("aftp" & ModuleId)
            Dim settings As Hashtable
            If obj Is Nothing Then
                settings = DotNetNuke.Entities.Portals.PortalSettings.GetModuleSettings(ModuleId)
                DataCache.CacheStore("aftp" & ModuleId, settings)
            Else
                settings = CType(obj, Hashtable)
            End If

            If Not CType(settings("AFTopPostsForums"), String) Is Nothing Then
                forumids = CType(settings("AFTopPostsForums"), String)
            End If
            If Not CType(settings("AFTopPostsNumber"), String) Is Nothing Then
                Rows = CType(settings("AFTopPostsNumber"), Integer)
            End If
            If Not CType(settings("AFTopPostsFormat"), String) Is Nothing Then
                Template = CType(settings("AFTopPostsFormat"), String)
            End If
            If Not CType(settings("AFTopPostsHeader"), String) Is Nothing Then
                Header = CType(settings("AFTopPostsHeader"), String)
            End If
            If Not CType(settings("AFTopPostsFooter"), String) Is Nothing Then
                Footer = CType(settings("AFTopPostsFooter"), String)
            End If
            If Not CType(settings("AFTopPostsTags"), String) Is Nothing Then
                Tags = CType(settings("AFTopPostsTags"), String)
            End If
            If Not CType(settings("AFTopPostsRSS"), String) Is Nothing Then
                bRSS = CType(settings("AFTopPostsRSS"), Boolean)
            End If
            If Not CType(settings("AFTopPostsTopicsOnly"), String) Is Nothing Then
                TopicsOnly = CType(settings("AFTopPostsTopicsOnly"), Boolean)
            End If
            If Not CType(settings("AFTopPostsRandomOrder"), String) Is Nothing Then
                RandomOrder = CType(settings("AFTopPostsRandomOrder"), Boolean)
            End If
            If Not CType(settings("AFTopPostsSecurity"), String) Is Nothing Then
                bRSSSecurity = CType(settings("AFTopPostsSecurity"), Boolean)
            End If
            If Not CType(settings("AFTopPostsBody"), String) Is Nothing Then
                bRSSIncludBody = CType(settings("AFTopPostsBody"), Boolean)
            End If
            'If Not CType(settings("AFTopPostsCache"), String) Is Nothing Then
            '    txtCache.Text = CType(settings("AFTopPostsCache"), String)
            'End If
            Dim uc As New UserController
            Dim u As User = uc.GetUser(PortalId, -1)
            Dim db As New Data.Common
            forumids = db.CheckForumIdsForView(forumids, u.UserRoles)

            Dim dr As IDataReader = DataProvider.Instance.GetPosts(PortalId, forumids, TopicsOnly, RandomOrder, Rows, UserId, False, UserInfo.IsSuperUser, Tags)
            sb.Append(Header)
            Dim BodyLength As Integer = -1
            Dim BodyTrim As String = ""
            If Template.Contains("[BODY:") Then
                Dim inStart As Integer = InStr(Template, "[BODY:") + 5
                Dim inEnd As Integer = InStr(inStart, Template, "]") - 1
                Dim sLength As String = Template.Substring(inStart, inEnd - inStart)
                BodyLength = CType(sLength, Integer)
                BodyTrim = "[BODY:" & BodyLength.ToString & "]"
            End If
            Dim useFriendly As Boolean = Utilities.IsRewriteLoaded()
            Dim sHost As String = Utilities.GetHost()
            Try
                While dr.Read
                    Dim sTempTemplate As String = Template
                    Dim GroupName As String = String.Empty
                    Dim GroupId As String = String.Empty
                    Dim TopicTabId As String = String.Empty
                    Dim TopicModuleId As String = String.Empty
                    Dim ForumName As String = String.Empty
                    Dim ForumId As String = String.Empty
                    Dim Subject As String = String.Empty
                    Dim UserName As String = String.Empty
                    Dim PostDate As String = String.Empty
                    Dim Body As String = String.Empty
                    Dim BodyHTML As String = String.Empty
                    Dim DisplayName As String = String.Empty
                    Dim ReplyCount As String = String.Empty
                    Dim LastPostDate As String = String.Empty
                    Dim TopicId As String = String.Empty
                    Dim ReplyId As String = String.Empty
                    Dim FirstName As String = String.Empty
                    Dim LastName As String = String.Empty
                    Dim AuthorId As String = String.Empty

                    Dim sForumUrl As String = String.Empty
                    Dim sTopicURL As String = String.Empty
                    Dim sGroupPrefixURL As String = String.Empty

                    GroupName = CStr(dr("GroupName"))
                    GroupId = CStr(dr("ForumGroupId"))
                    TopicTabId = CStr(dr("TabId"))
                    TopicModuleId = CStr(dr("ModuleId"))
                    ForumName = CStr(dr("ForumName"))
                    ForumId = CStr(dr("ForumId"))
                    Subject = CStr(dr("Subject"))
                    UserName = CStr(dr("AuthorUserName"))
                    FirstName = CStr(dr("AuthorFirstName"))
                    LastName = CStr(dr("AuthorLastName"))
                    AuthorId = CStr(dr("AuthorId"))
                    DisplayName = CStr(dr("AuthorDisplayName"))
                    PostDate = CStr(dr("DateCreated"))
                    Body = Utilities.StripHTMLTag(CStr(dr("Body")))
                    TopicId = CStr(dr("TopicId"))
                    ReplyId = CStr(dr("ReplyId"))
                    BodyHTML = CStr(dr("Body"))
                    ReplyCount = CStr(dr("ReplyCount"))

                    sForumUrl = dr("PrefixURL").ToString
                    sTopicURL = dr("TopicURL").ToString
                    sGroupPrefixURL = dr("GroupPrefixURL").ToString

                    Dim ts As SettingsInfo = DataCache.MainSettings(CInt(TopicModuleId))
                    sTempTemplate = sTempTemplate.Replace("[FORUMGROUPNAME]", GroupName)
                    sTempTemplate = sTempTemplate.Replace("[FORUMGROUPID]", GroupId)
                    sTempTemplate = sTempTemplate.Replace("[TOPICTABID]", TopicTabId)
                    sTempTemplate = sTempTemplate.Replace("[TOPICMODULEID]", TopicModuleId)
                    sTempTemplate = sTempTemplate.Replace("[FORUMNAME]", ForumName)
                    sTempTemplate = sTempTemplate.Replace("[FORUMID]", ForumId)
                    sTempTemplate = sTempTemplate.Replace("[SUBJECT]", Subject)
                    sTempTemplate = sTempTemplate.Replace("[AUTHORUSERNAME]", UserName)
                    sTempTemplate = sTempTemplate.Replace("[AUTHORFIRSTNAME]", FirstName)
                    sTempTemplate = sTempTemplate.Replace("[AUTHORLASTNAME]", LastName)
                    sTempTemplate = sTempTemplate.Replace("[AUTHORID]", AuthorId)
                    sTempTemplate = sTempTemplate.Replace("[AUTHORDISPLAYNAME]", DisplayName)
                    sTempTemplate = sTempTemplate.Replace("[DATE]", Utilities.GetDate(CDate(PostDate), CInt(TopicModuleId), timeOffset))
                    sTempTemplate = sTempTemplate.Replace("[BODY]", Body)
                    sTempTemplate = sTempTemplate.Replace("[BODYHTML]", BodyHTML)
                    sTempTemplate = sTempTemplate.Replace("[BODYTEXT]", Utilities.StripHTMLTag(BodyHTML))
                    If Not BodyTrim = String.Empty Then
                        If BodyLength > 0 And Body.Length > BodyLength Then
                            sTempTemplate = sTempTemplate.Replace(BodyTrim, Body.Substring(0, BodyLength))
                        Else
                            sTempTemplate = sTempTemplate.Replace(BodyTrim, Body)
                        End If
                    End If

                    sTempTemplate = sTempTemplate.Replace("[TOPICID]", TopicId)
                    sTempTemplate = sTempTemplate.Replace("[REPLYID]", ReplyId)
                    sTempTemplate = sTempTemplate.Replace("[REPLYCOUNT]", ReplyCount)

                    'Dim Expression As New Text.RegularExpressions.Regex("[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$")
                    'If Expression.IsMatch(Subject) Then
                    '    Subject = Subject.Replace(Expression.Match(Subject).Value, String.Empty)
                    'End If



                    'UserProfiles.GetDisplayName(CInt(AuthorId), ts.UserNameDisplay, False, UserName, FirstName, LastName, DisplayName)
                    If useFriendly And Not (String.IsNullOrEmpty(sForumUrl) And String.IsNullOrEmpty(sTopicURL)) Then
                        Dim ctlUtils As New ControlUtils
                        sTopicURL = ctlUtils.BuildUrl(CInt(TopicTabId), CInt(TopicModuleId), sGroupPrefixURL, sForumUrl, CInt(GroupId), CInt(ForumId), CInt(TopicId), sTopicURL, -1, -1, String.Empty, 1, -1)
                        sForumUrl = ctlUtils.BuildUrl(CInt(TopicTabId), CInt(TopicModuleId), sGroupPrefixURL, sForumUrl, CInt(GroupId), CInt(ForumId), -1, String.Empty, -1, -1, String.Empty, 1, -1)
                        If sHost.EndsWith("/") And sForumUrl.StartsWith("/") Then
                            sForumUrl = sForumUrl.Substring(1)
                        End If
                        If Not sForumUrl.StartsWith(sHost) Then
                            sForumUrl = sHost & sForumUrl
                        End If

                        If sHost.EndsWith("/") And sTopicURL.StartsWith("/") Then
                            sTopicURL = sTopicURL.Substring(1)
                        End If
                        If Not sTopicURL.StartsWith(sHost) Then
                            sTopicURL = sHost & sTopicURL
                        End If

                        If CInt(ReplyId) = 0 Then
                            sTempTemplate = sTempTemplate.Replace("[POSTURL]", sTopicURL)
                            sTempTemplate = sTempTemplate.Replace("[SUBJECTLINK]", "<a href=""" & sTopicURL & """>" & Subject & "</a>")
                        Else
                            If Not sTopicURL.EndsWith("/") And Not sTopicURL.EndsWith("aspx") Then
                                sTopicURL &= "/"
                            End If
                            sTopicURL &= "?afc=" & ReplyId
                            sTempTemplate = sTempTemplate.Replace("[POSTURL]", sTopicURL)
                            If Request.IsAuthenticated Then
                                sTempTemplate = sTempTemplate.Replace("[SUBJECTLINK]", "<a href=""" & sTopicURL & """>" & Subject & "</a>")
                            Else
                                sTempTemplate = sTempTemplate.Replace("[SUBJECTLINK]", "<a href=""" & sTopicURL & """ rel=""nofollow"">" & Subject & "</a>")
                            End If
                        End If
                        sTempTemplate = sTempTemplate.Replace("[TOPICSURL]", sForumUrl)
                    Else
                        If CInt(ReplyId) = 0 Then
                            sTempTemplate = sTempTemplate.Replace("[POSTURL]", DotNetNuke.Common.NavigateURL(CInt(TopicTabId), "", New String() {ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId}))
                            sTempTemplate = sTempTemplate.Replace("[SUBJECTLINK]", "<a href=""" & DotNetNuke.Common.NavigateURL(CInt(TopicTabId), "", New String() {ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId}) & """>" & Subject & "</a>")
                        Else
                            sTempTemplate = sTempTemplate.Replace("[POSTURL]", DotNetNuke.Common.NavigateURL(CInt(TopicTabId), "", New String() {ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ContentJumpId & "=" & ReplyId}))
                            sTempTemplate = sTempTemplate.Replace("[SUBJECTLINK]", "<a href=""" & DotNetNuke.Common.NavigateURL(CInt(TopicTabId), "", New String() {ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ContentJumpId & "=" & ReplyId}) & """>" & Subject & "</a>")
                        End If
                        sTempTemplate = sTempTemplate.Replace("[TOPICSURL]", DotNetNuke.Common.NavigateURL(CInt(TopicTabId), "", New String() {ParamKeys.ViewType & "=" & Views.Topics, ParamKeys.ForumId & "=" & ForumId}))
                    End If


                    sTempTemplate = sTempTemplate.Replace("[FORUMURL]", DotNetNuke.Common.NavigateURL(CInt(TopicTabId)))
                    sb.Append(sTempTemplate)


                End While
                dr.Close()
            Catch ex As Exception
                If Not dr.IsClosed Then
                    dr.Close()
                End If
                sb.Append(ex.StackTrace)
            End Try
            Dim sRSSImage As String = String.Empty
            Dim sRSSURL As String = String.Empty
            Dim sRSSIconLink As String = String.Empty
            If bRSS Then
                sRSSImage = "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/feedicon.gif") & """ border=""0"" />"
                sRSSURL = Page.ResolveUrl("~/desktopmodules/activeforumswhatsnew/feeds.aspx") & "?portalId=" & PortalId & "&tabid=" & TabId.ToString & "&moduleid=" & ModuleId.ToString
                sRSSIconLink = "<a href=""" & sRSSURL & """>" & sRSSImage & "</a>"
            End If
            Footer = Footer.Replace("[RSSICON]", sRSSImage)
            Footer = Footer.Replace("[RSSURL]", sRSSURL)
            Footer = Footer.Replace("[RSSICONLINK]", sRSSIconLink)

            sb.Append(Footer)
            lit.Text = sb.ToString
            Me.Controls.Add(lit)
        End Sub
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
            Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
            MyBase.Render(htmlWriter)
            Dim html As String = stringWriter.ToString()
            html = Utilities.LocalizeControl(html, "WhatsNew")
            writer.Write(html)
        End Sub
    End Class
End Namespace
